package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.HouseMapper;
import com.weipai.demo.mapper.HouseTypeMapper;
import com.weipai.demo.model.House;
import com.weipai.demo.model.HouseType;
import com.weipai.demo.service.HouseService;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private HouseTypeMapper houseTypeMapper;


    @Override
    public Response getList(Integer page, Integer size, House house, Integer front,Integer sort,String sortType) {
        Response response = new Response();
        try {
            PageHelper.startPage(page, size);
            List<Map<String, Object>> list = houseMapper.getAll(house);
            if (front != null && front > 0 && list.size() > 0) {
                for (Map<String, Object> item: list) {
                    List<Map<String, Object>> list1 = houseTypeMapper.getListByHouseId((Integer)item.get("id"));
                    if(list1 != null){
                        item.put("children",list1);
                    }
                }
            }
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            response.setCode(RetCode.SUCCESS);
            response.setCount(pageInfo.getTotal());
            response.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(RetCode.REQUEST_ERROR);
            response.setData(e.getMessage());
//            logger.error("show" + e.getMessage());
        }
        return response;
    }


    @Override
    public Response getHouseTypeList(Integer page, Integer size, Integer id) {
        Response response = new Response();
        try {
            if (id == null) {
                response.setCode(RetCode.REQUEST_ERROR);
                return response;
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> list = houseTypeMapper.getListByHouseId(id);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            response.setCode(RetCode.SUCCESS);
            response.setCount(pageInfo.getTotal());
            response.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(RetCode.REQUEST_ERROR);
            response.setData(e.getMessage());
//            logger.error("show" + e.getMessage());
        }
        return response;
    }

    @Override
    public Response addHouse(House house) {
        Response response = new Response();
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = outFormat.format(now);
        house.setCreateTime(s);
        int rest = 0;
        if (house.getId() != null) {
            rest = houseMapper.updateByPrimaryKey(house);
        } else {
            rest = houseMapper.insert(house);
        }
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response addHouseType(HouseType houseType) {
        Response response = new Response();
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = outFormat.format(now);
        houseType.setCreateTime(s);
        int rest = 0;
        if (houseType.getId() != null) {
            rest = houseTypeMapper.updateByPrimaryKey(houseType);
        } else {
            rest = houseTypeMapper.insert(houseType);
        }
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response delHouse(Integer id) {
        Response response = new Response();
        if (id == null) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        List<Map<String, Object>> list = houseTypeMapper.getListByHouseId(id);
        if (list.size() > 0) {
            response.setCode(RetCode.DELHOUSE);
            return response;
        }
        int rest = houseMapper.deleteByPrimaryKey(id);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response delHouseType(Integer id) {
        Response response = new Response();
        if (id == null) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        int rest = houseTypeMapper.deleteByPrimaryKey(id);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response changePublishStatus(Integer id, Integer publishStatus) {
        Response response = new Response();
        if (id == null) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        int rest = houseMapper.changePublishStatus(id, publishStatus);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.RESOURCES_ADD_FAIL);
        }
        return response;
    }
    @Override
    public Response getDetail(Integer id) {
        Response response = new Response();
        try {

            Map<String, Object> list = houseMapper.getOne(id);
            List<Map<String, Object>> list1 = houseTypeMapper.getListByHouseId((Integer) list.get("id"));
            if (list1 != null) {
                list.put("children", list1);
            }
            response.setCode(RetCode.SUCCESS);
            response.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(RetCode.REQUEST_ERROR);
            response.setData(e.getMessage());
//            logger.error("show" + e.getMessage());
        }
        return response;
    }
}
